import time
from Case.python_ui.PageLocate.system_setting_locator import SystemSettinglocator
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.Common.commontool import CommonTool

class SystemSettingPage(Basepage):

    # 判断是否包含元素
    def check_contain_str(self, string, except_string):
        if except_string in string:
            assert True
        else:
            assert False, '实际值:' + string + '期望值:' + except_string

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result




    def enter_system_setting_page(self):
        doc='进入到systemsetting页面'
        for i in range(3):
            try:
                eles = self.get_elements(SystemSettinglocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(SystemSettinglocator.system_setting_tab, doc)
                self.click_element(SystemSettinglocator.system_setting_tab, doc)
                self.wait_elevisble(SystemSettinglocator.custom_setting_tab, doc)
                self.click_element(SystemSettinglocator.custom_setting_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(SystemSettinglocator.custom_setting_tab,doc)


    def enter_account_setting_page(self):
        doc = '进入到accountsetting页面'
        for i in range(3):
            try:
                eles = self.get_elements(SystemSettinglocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(SystemSettinglocator.account_setting_tab, doc)
                self.click_element(SystemSettinglocator.account_setting_tab, doc)
                self.wait_elevisble(SystemSettinglocator.basic_information_tab, doc)
                self.click_element(SystemSettinglocator.basic_information_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(SystemSettinglocator.edit_button,doc)


    def enter_authorization_lisence_page(self):
        doc = '进入Authorization License页面'
        for i in range(3):
            try:
                eles = self.get_elements(SystemSettinglocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(SystemSettinglocator.system_setting_tab, doc)
                self.click_element(SystemSettinglocator.system_setting_tab, doc)
                self.wait_elevisble(SystemSettinglocator.authorization_license_menu, doc)
                self.click_element(SystemSettinglocator.authorization_license_menu, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(SystemSettinglocator.authorization_license_menu,doc)


    def admin_interface_appearance(self):
        doc='admin用户界面外观'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.admim_appearace_site,doc)
        self.wait_elevisble(SystemSettinglocator.public_confirm_button,doc)
        time.sleep(2)
        element=self.get_element(SystemSettinglocator.pot_theme,doc)
        class_value=element.get_attribute('class')
        print(class_value)
        return class_value

    def user_interface_appearance(self):
        doc='user用户界面外观'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.admim_appearace_site,doc)
        self.wait_elevisble(SystemSettinglocator.public_confirm_button,doc)
        time.sleep(2)

    def get_potal_theme(self):
        doc='获取主题'
        element=self.get_element(SystemSettinglocator.pot_theme,doc)
        class_value=element.get_attribute('class')
        print(class_value)
        return class_value

    def modify_theme_color(self):
        doc = 'admin用户界面主题颜色'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.color_site, doc)
        self.wait_elevisble(SystemSettinglocator.public_confirm_button, doc)
        time.sleep(2)
        theme_color_script = """
        return window.getComputedStyle(document.body, null).getPropertyValue("background-color")
        """
        theme_color=self.excute_js(theme_color_script)
        print(theme_color)
        return theme_color

    def set_system_exchange_rate(self):
        doc='admin端设置系统汇率'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.system_exchange,doc)
        self.wait_elevisble(SystemSettinglocator.exchange_edit,doc)
        self.click_element(SystemSettinglocator.exchange_edit,doc)
        self.wait_elevisble(SystemSettinglocator.exchange_rate_add,doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.exchange_rate_add,doc)
        self.wait_elevisble(SystemSettinglocator.converted_currency_choice,doc)
        self.click_element(SystemSettinglocator.converted_currency_choice,doc)
        self.wait_elevisble(SystemSettinglocator.converted_currency_option,doc)
        eles=self.get_elements(SystemSettinglocator.converted_currency_option,doc)
        eles[0].click()
        self.send_keys(SystemSettinglocator.current_value,'1',doc)
        self.send_keys(SystemSettinglocator.exchange_value, '1', doc)
        self.click_element(SystemSettinglocator.confirm_button,doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.save_button,doc)
        time.sleep(2)


    def delete_system_exchange_rate(self):
        doc = '删除admin端设置系统汇率'
        self.wait_elevisble(SystemSettinglocator.exchange_edit, doc)
        self.click_element(SystemSettinglocator.exchange_edit, doc)
        self.wait_elevisble(SystemSettinglocator.exchange_rate_add, doc)
        time.sleep(2)
        eles=self.get_elements(SystemSettinglocator.delete_button,doc)
        eles[-1].click()
        self.wait_elevisble(SystemSettinglocator.delete_ack,doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.delete_ack,doc)
        time.sleep(1)
        self.click_element(SystemSettinglocator.public_confirm_button,doc)

    def view_system_exchange_rate_hiostory(self):
        doc='查看汇率历史'
        self.refresh_page()
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.system_exchange, doc)
        self.wait_elevisble(SystemSettinglocator.exchange_view, doc)
        self.click_element(SystemSettinglocator.exchange_view, doc)
        self.wait_elevisble(SystemSettinglocator.exchange_rate_history,doc)
        excange_rate=self.get_element(SystemSettinglocator.exchange_rate_history,doc).text
        return excange_rate


    def page_dispaly(self):
        doc='accountsetting页面展示'
        self.enter_account_setting_page()
        account_email=self.get_element(SystemSettinglocator.account_email,doc).text
        return account_email


    def change_phone_number(self,cmdopt):
        doc = 'accountsetting修改手机号'
        self.enter_account_setting_page()
        self.wait_elevisble(SystemSettinglocator.edit_button,doc)
        self.click_element(SystemSettinglocator.edit_button,doc)
        self.wait_elevisble(SystemSettinglocator.verification_code,doc)
        self.send_keys(SystemSettinglocator.verification_code,Logindata.verification_code[cmdopt]['code'],doc)
        self.click_element(SystemSettinglocator.save_button,doc)
        new_phone_number=CommonTool.generate_phone_number()
        self.wait_elevisble(SystemSettinglocator.new_phone_number,doc)
        time.sleep(2)
        self.send_keys(SystemSettinglocator.new_phone_number,new_phone_number,doc)
        self.send_keys(SystemSettinglocator.new_verification_code,Logindata.verification_code[cmdopt]['code'],doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.save_button,doc)


    def edit_contact_information(self):
        doc='accountsetting修改联系人信息'
        self.enter_account_setting_page()
        self.wait_elevisble(SystemSettinglocator.contact_deit_button, doc)
        self.click_element(SystemSettinglocator.contact_deit_button,doc)
        time.sleep(2)
        address=CommonTool.generate_string()
        self.keyboard_event(SystemSettinglocator.address_input,doc)
        self.send_keys(SystemSettinglocator.address_input,address,doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.save_button,doc)
        return address

    def get_address_text(self):
        doc='获取'
        time.sleep(2)
        eles=self.get_elements(SystemSettinglocator.address_text,doc)
        check_address=eles[-1].text
        return check_address



    def check_authorization_license(self):
        doc = 'Licensing Details of Cloud Revenue Engine列表'
        self.enter_authorization_lisence_page()
        self.check_attribute(self.get_element(SystemSettinglocator.authorization_version, doc).text, 'Authorization Version')
        self.check_attribute(self.get_element(SystemSettinglocator.authorization_company, doc).text, 'Authorization Company')
        self.check_attribute(self.get_element(SystemSettinglocator.authorization_type, doc).text, 'Authorization Type')
        self.check_attribute(self.get_element(SystemSettinglocator.authorization_created_at, doc).text, 'Authorization Created At')
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.name, doc).text, 'Name')
        self.check_attribute(self.get_element(SystemSettinglocator.software_content, doc).text, 'Software Content')
        self.check_attribute(self.get_element(SystemSettinglocator.unit, doc).text, 'Unit')
        self.check_attribute(self.get_element(SystemSettinglocator.authorization_status, doc).text, 'Authorizations Status')



    def edit_documeentation_for_admin(self):
        doc = 'Documentation for Admin选项'
        note = 'Modify this jump link, the affected area as shown below'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.jump_link_tab, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.documentation_for_admin_edit_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.documentation_note, doc).text, note)



    def edit_tip_bar_log_for_user(self):
        doc = 'Top Bar Logo for User选项'
        note = 'Modify this jump link, the affected area as shown below'
        link = 'https://testcre.aligy.cn/test/homepage/marketplace'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.jump_link_tab, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.top_bar_logo_for_user_edit_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.documentation_note, doc).text, note)
        result = self.get_element(SystemSettinglocator.link_address, doc).text
        if link ==result:
            return True
        else:
            self.keyboard_event(SystemSettinglocator.link_address, doc)
            time.sleep(2)
            self.send_keys(SystemSettinglocator.link_address, link, doc)



#
    def edit_documeentation_for_user(self):
        doc = 'Documentation for User选项'
        note = 'Modify this jump link, the affected area as shown below'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.jump_link_tab, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.documentation_for_personal_user_edit, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.documentation_note, doc).text, note)
        self.click_element(SystemSettinglocator.cancel_button, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.documentation_for_enterprise_user_edit, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.documentation_note2, doc).text, note)



    def check_zone_list(self):
        doc = 'Zone页面字段效验'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.zone_tab, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.sort, doc).text, 'Sort')
        self.check_attribute(self.get_element(SystemSettinglocator.zone_id, doc).text, 'Zone ID')
        self.check_attribute(self.get_element(SystemSettinglocator.zone_name, doc).text, 'Zone Name')
        self.check_attribute(self.get_element(SystemSettinglocator.description, doc).text, 'Description')
        self.check_attribute(self.get_element(SystemSettinglocator.actions, doc).text, 'Actions')



    def zone_list_search(self):
        doc = '搜索字段检索查询'
        id = 'id-jakarta-testbed-am1662001-a-x'
        name = 'BSD new'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.zone_tab, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.region_select, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.region_first_input, doc)
        time.sleep(2)
        self.send_keys(SystemSettinglocator.zone_id_name_input, id, doc)
        self.click_element(SystemSettinglocator.zone_list_search_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.id_result, doc).text, id)
        self.keyboard_event(SystemSettinglocator.zone_id_name_input, doc)
        self.send_keys(SystemSettinglocator.zone_id_name_input, name, doc)
        self.click_element(SystemSettinglocator.zone_list_search_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.name_result, doc).text, name)



    def zone_list_edit(self):
        doc = '编辑区域信息'
        id = 'id-jakarta-testbed-am1662001-a-x'
        name = 'BSD new'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.zone_tab, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.region_select, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.region_first_input, doc)
        time.sleep(2)
        self.send_keys(SystemSettinglocator.zone_id_name_input, id, doc)
        self.click_element(SystemSettinglocator.zone_list_search_button, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.zone_list_edit_button, doc)
        time.sleep(2)
        self.keyboard_event(SystemSettinglocator.zone_name_input, doc)
        self.send_keys(SystemSettinglocator.zone_name_input, 'test', doc)
        self.click_element(SystemSettinglocator.I_acknoeledge_button, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.ok_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.name_result, doc).text, 'test')
        self.click_element(SystemSettinglocator.zone_list_edit_button, doc)
        time.sleep(2)
        self.keyboard_event(SystemSettinglocator.zone_name_input, doc)
        self.send_keys(SystemSettinglocator.zone_name_input, name, doc)
        self.click_element(SystemSettinglocator.I_acknoeledge_button, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.ok_button, doc)


    def check_region_list(self):
        doc = '区域列表页面字段效验'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.region_tab, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.sort, doc).text, 'Sort')
        self.check_attribute(self.get_element(SystemSettinglocator.region_id, doc).text, 'Region ID')
        self.check_attribute(self.get_element(SystemSettinglocator.region_name, doc).text, 'Region Name')
        self.check_attribute(self.get_element(SystemSettinglocator.description, doc).text, 'Description')
        self.check_attribute(self.get_element(SystemSettinglocator.billing_mode, doc).text, 'Billing Mode')
        self.check_attribute(self.get_element(SystemSettinglocator.support_price_modification, doc).text, 'Support Price Modification')
        self.check_attribute(self.get_element(SystemSettinglocator.currency, doc).text, 'Currency')




    def region_list_search(self):
        doc = '搜索字段检索查询'
        name = 'id-jakarta-testbed-d01'
        id = 'id-jakarta-testbed-d01'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.region_tab, doc)
        time.sleep(2)
        self.wait_elevisble(SystemSettinglocator.region_id_name_input, doc)
        self.send_keys(SystemSettinglocator.region_id_name_input, name, doc)
        self.click_element(SystemSettinglocator.region_list_search_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.name_ffirst_result, doc).text, name)
        self.keyboard_event(SystemSettinglocator.region_id_name_input, doc)
        self.send_keys(SystemSettinglocator.region_id_name_input, id, doc)
        self.click_element(SystemSettinglocator.region_list_search_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.id_first_result, doc).text, id)




    def region_list_edit(self):
        doc = '编辑区域信息'
        id = 'id-jakarta-testbed-d01'
        name = 'id-jakarta-testbed-d01'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.region_tab, doc)
        time.sleep(2)
        self.send_keys(SystemSettinglocator.region_id_name_input, id, doc)
        self.click_element(SystemSettinglocator.region_list_search_button, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.zone_list_edit_button, doc)
        time.sleep(2)
        self.keyboard_event(SystemSettinglocator.zone_name_input, doc)
        self.send_keys(SystemSettinglocator.zone_name_input, 'test', doc)
        self.click_element(SystemSettinglocator.I_acknoeledge_button, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.ok_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.name_ffirst_result, doc).text, 'test')
        self.click_element(SystemSettinglocator.zone_list_edit_button, doc)
        time.sleep(2)
        self.keyboard_event(SystemSettinglocator.zone_name_input, doc)
        self.send_keys(SystemSettinglocator.zone_name_input, name, doc)
        self.click_element(SystemSettinglocator.I_acknoeledge_button, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.ok_button, doc)



    def edit_level_setting(self):
        doc = '经销商用户折扣级别设置'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.initialization_settings_tab, doc)
        time.sleep(5)
        self.click_element(SystemSettinglocator.level_setting_edit_buttton, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.add_button, doc)
        time.sleep(2)
        self.send_keys(SystemSettinglocator.reseller_level_input, 'test', doc)
        self.send_keys(SystemSettinglocator.discount_range_input, 10, doc)
        self.click_element(SystemSettinglocator.ok_button, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.level_setting_edit_buttton, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.level_delete_button, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.ok_button, doc)




    def add_edit_social_media(self):
        doc = '新增/编辑社交媒体'
        url = 'http://test'
        url_new = 'http://test1'
        self.enter_system_setting_page()
        self.click_element(SystemSettinglocator.social_media_tab, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.social_media_create_button, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.social_media_select, doc)
        time.sleep(2)
        self.click_element(SystemSettinglocator.social_media_first_select, doc)
        time.sleep(2)
        self.send_keys(SystemSettinglocator.url_input, url, doc)
        self.click_element(SystemSettinglocator.ok_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.url_result, doc).text, url)
        time.sleep(2)
        self.click_element(SystemSettinglocator.social_media_edit_button, doc)
        time.sleep(2)
        self.keyboard_event(SystemSettinglocator.url_input, doc)
        self.send_keys(SystemSettinglocator.url_input, url_new, doc)
        self.click_element(SystemSettinglocator.ok_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(SystemSettinglocator.url_result, doc).text, url_new)
        self.click_element(SystemSettinglocator.social_media_delete_button, doc)
        time.sleep(2)

























